package com.qdl.rym.config;

import com.qdl.rym.base.ResultModel;
import com.qdl.rym.constants.BaseEnums;
import com.qdl.rym.exception.AuthorityException;
import com.qdl.rym.exception.BaseException;
import com.qdl.rym.exception.BusinessException;
import com.qdl.rym.exception.ServiceException;
import com.qdl.rym.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Description: GlobalExceptionConfig
 * 全局异常处理
 * @Author: ChenXueLi
 * @Date: 2018-11-10 18:51
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionConfig {

    /**
     * 处理 BusinessException 异常
     */
    @ExceptionHandler(BusinessException.class)
    public ResultModel handleBusinessException(BusinessException e){
        ResultModel resultModel = ResultUtil.failure(e.getCode(), e.getMessage());
        resultModel.setCode(e.getCode());
//        resultModel.setCode(HttpStatus.BAD_REQUEST.value());
        log.info("BusinessException[code: {}, message: {}]", e.getCode(), e.getMessage());
        return resultModel;
    }

    /**
     * 处理 ServiceException 异常
     */
    @ExceptionHandler(ServiceException.class)
    public ResultModel handleServiceException(ServiceException e){
        ResultModel resultModel = ResultUtil.failure(e.getCode(), e.getMessage());
        resultModel.setCode(HttpStatus.BAD_REQUEST.value());
        log.info("ServiceException[code: {}, message: {}]", e.getCode(), e.getMessage());
        return resultModel;
    }

    /**
     * 处理 AuthorityException 异常
     */
    @ExceptionHandler(AuthorityException.class)
    public ResultModel handleAuthorityException(AuthorityException e){
        ResultModel resultModel = ResultUtil.failure(BaseEnums.FORBIDDEN.code(), BaseEnums.FORBIDDEN.desc());
        resultModel.setCode(HttpStatus.FORBIDDEN.value());
        log.info("AuthorityException[code: {}, message: {}]", e.getCode(), e.getMessage());
        return resultModel;
    }

    /**
     * 处理 NoHandlerFoundException 异常
     * 需配置 [spring.mvc.throw-exception-if-no-handler-found=true]
     * 需配置 [spring.resources.add-mappings=false]
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResultModel handleNotFoundException(NoHandlerFoundException e){
        ResultModel resultModel = ResultUtil.failure(BaseEnums.NOT_FOUND.code(), BaseEnums.NOT_FOUND.desc());
        resultModel.setCode(HttpStatus.NOT_FOUND.value());
        log.info(e.getMessage());
        return resultModel;
    }

    /**
     * 处理 BaseException 异常
     */
    @ExceptionHandler(BaseException.class)
    public ResultModel handleBaseException(BaseException e){
        ResultModel resultModel = ResultUtil.failure(e.getCode(), e.getMessage());
        resultModel.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        log.error("BaseException[code: {}, message: {}]", e.getCode(), e.getMessage(), e);
        return resultModel;
    }

    /**
     * 处理 Exception 异常
     */
    @ExceptionHandler(Exception.class)
    public ResultModel handleException(Exception e){
        ResultModel resultModel = ResultUtil.failure(BaseEnums.ERROR.code(), BaseEnums.ERROR.desc());
        resultModel.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        log.error(e.getMessage(), e);
        return resultModel;
    }

}
